# coding: utf-8
from django.contrib.auth.decorators import login_required
from django.http import Http404, HttpResponseForbidden
from models import SharedAccount

def member_required(view):
    def _wrapper_view(request, id, *args, **kwargs):
        id = long(id)
        account = SharedAccount.get_by_id(id)
        if account is None:
            raise Http404
        current_member = None
        for member in account.get_members():
            if member.user.id == request.user.id:
                current_member = member
                break
        if current_member is None:
            return HttpResponseForbidden(u'您无权访问')
        return view(request, account, current_member, *args, **kwargs)
    return login_required(_wrapper_view)

